package com.TIEmulator.FileIOLib;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FileBrowser
  implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener
{
  private static final String mStartDirectory = "/sdcard";
  private File currentDirectory = null;
  private final List<IconifiedText> directoryEntries = new ArrayList();
  private Context mContext = null;
  private ListView mListview = null;
  private float mSize = 20.0F;
  private TextView mTopBar = null;

  public FileBrowser(Context paramContext, ListView paramListView, TextView paramTextView, String paramString)
  {
    this.mTopBar = paramTextView;
    this.mListview = paramListView;
    this.mContext = paramContext;
    this.mListview.setOnItemClickListener(this);
    this.mListview.setOnItemLongClickListener(this);
    if (paramString == null)
      paramString = "/sdcard";
    this.currentDirectory = new File(paramString);
    if (!(isChild(this.currentDirectory)))
      this.currentDirectory = new File("/sdcard");
    browseTo(this.currentDirectory);
  }

  private void fill(File[] paramArrayOfFile)
  {
    this.directoryEntries.clear();
    int i = paramArrayOfFile.length;
    int j = 0;
    if (j >= i)
    {
      label14: Collections.sort(this.directoryEntries);
      IconifiedTextListAdapter localIconifiedTextListAdapter = new IconifiedTextListAdapter(this.mContext);
      localIconifiedTextListAdapter.setTextSize(this.mSize);
      localIconifiedTextListAdapter.setListItems(this.directoryEntries);
      this.mListview.setAdapter(localIconifiedTextListAdapter);
      return;
    }
    File localFile = paramArrayOfFile[j];
    boolean bool = localFile.isDirectory();
    if (bool);
    for (Drawable localDrawable = this.mContext.getResources().getDrawable(2130837541); ; localDrawable = this.mContext.getResources().getDrawable(2130837560))
      while (true)
      {
        int k = 1 + this.currentDirectory.getAbsolutePath().length();
        IconifiedText localIconifiedText = new IconifiedText(localFile.getAbsolutePath().substring(k), localDrawable);
        localIconifiedText.setSelectable(bool);
        this.directoryEntries.add(localIconifiedText);
        ++j;
        break label14:
        String str = localFile.getName();
        if (FileIOLib.checkEndsWithInStringArray(str, this.mContext.getResources().getStringArray(2131099648)))
          localDrawable = this.mContext.getResources().getDrawable(2130837544);
        if (FileIOLib.checkEndsWithInStringArray(str, this.mContext.getResources().getStringArray(2131099651)))
          localDrawable = this.mContext.getResources().getDrawable(2130837566);
        if (FileIOLib.checkEndsWithInStringArray(str, this.mContext.getResources().getStringArray(2131099650)))
          localDrawable = this.mContext.getResources().getDrawable(2130837554);
        if (FileIOLib.checkEndsWithInStringArray(str, this.mContext.getResources().getStringArray(2131099649)))
          localDrawable = this.mContext.getResources().getDrawable(2130837508);
        if (!(FileIOLib.checkEndsWithInStringArray(str, this.mContext.getResources().getStringArray(2131099652))))
          break;
        localDrawable = this.mContext.getResources().getDrawable(2130837531);
      }
  }

  private boolean isChild(File paramFile)
  {
    if ((paramFile.getParent() != null) && (paramFile.getParent().contains("/sdcard")));
    for (int i = 1; ; i = 0)
      return i;
  }

  public void Update()
  {
    browseTo(this.currentDirectory);
  }

  public void ZoomIn()
  {
    this.mSize = (float)(1.2D * this.mSize);
    if (this.mSize > 40.0F)
      this.mSize = 40.0F;
    Update();
  }

  public void ZoomOut()
  {
    this.mSize = (float)(0.8D * this.mSize);
    if (this.mSize < 10.0F)
      this.mSize = 10.0F;
    Update();
  }

  public void browseTo(File paramFile)
  {
    if (!(paramFile.isDirectory()))
      return;
    Log.v(super.toString(), "browseTo is set to directory path : " + paramFile.getPath());
    this.mTopBar.setText(paramFile.toString());
    this.currentDirectory = paramFile;
    fill(paramFile.listFiles());
  }

  public int getDirectoryLength()
  {
    return this.currentDirectory.toString().length();
  }

  public String getDirectorySelected()
  {
    return this.currentDirectory.toString();
  }

  public float getZoom()
  {
    return this.mSize;
  }

  public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
  {
    File localFile = new File(this.currentDirectory.getAbsolutePath() + "/" + ((IconifiedText)this.directoryEntries.get((int)paramLong)).getText());
    if (localFile == null)
      return;
    browseTo(localFile);
  }

  public boolean onItemLongClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
  {
    if (((IconifiedText)this.directoryEntries.get((int)paramLong)).isSelectable())
    {
      File localFile = new File(this.currentDirectory.getAbsolutePath() + "/" + ((IconifiedText)this.directoryEntries.get((int)paramLong)).getText());
      if (localFile != null)
        browseTo(localFile);
      ((Interface)this.mContext).OnDirSelectedCb();
    }
    for (int i = 1; ; i = 0)
    {
      return i;
      ((Interface)this.mContext).OnFileSelectedCb(((IconifiedText)this.directoryEntries.get((int)paramLong)).getText().toString());
    }
  }

  public void setZoom(float paramFloat)
  {
    this.mSize = paramFloat;
    Update();
  }

  public void upOneLevel()
  {
    if (!(isChild(this.currentDirectory)))
      return;
    browseTo(this.currentDirectory.getParentFile());
  }

  public static abstract interface Interface
  {
    public abstract void OnDirSelectedCb();

    public abstract void OnFileSelectedCb(String paramString);
  }
}
